package com.example.app.components.MyDialog

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import com.example.app.cls.MyState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class MyfullScreenDialogState : MyState() {
    var _cotnent =
        MutableStateFlow<(@Composable (animateOffsetX: State<Dp>, windowWidth: Dp) -> Unit)?>(null)
    var cotnent = _cotnent.asStateFlow()
    fun flowUpdate_cotnent(value: @Composable (animateOffsetX: State<Dp>, windowWidth: Dp) -> Unit) {
        _cotnent.update {
            value
        }
    }
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyfullScreenDialog(myfullScreenDialogState: MyfullScreenDialogState,
    onDismissRequest: () -> Unit = { myfullScreenDialogState.flowUpdate_visible(false) },
    properties: DialogProperties = DialogProperties(usePlatformDefaultWidth = false,
        decorFitsSystemWindows = false)) {
    var visible = myfullScreenDialogState.visible.collectAsState()
    var windowWidth = with(LocalDensity.current) {
        LocalConfiguration.current.screenWidthDp
    }
    var animateOffsetX =
        animateDpAsState(targetValue = if (visible.value) 0.dp else -windowWidth.dp)
    if (visible.value) {
        var content = myfullScreenDialogState.cotnent.collectAsState()
        MyDialog(modifier = Modifier,
            onDismissRequest = onDismissRequest,
            properties = properties,
            content = {
                if (content.value != null) {
                    content.value!!(animateOffsetX,
                        windowWidth.dp)
                }
            })
    }
}